import { getServer, isLogin } from '/store/mutations'
import { GlobalData } from '/store/state'
// import { commonApi } from "/api/common";
import { servers } from '/utils/request'
import { $event } from '/utils'
import { checkAuth } from '/store/actions'

Page({
  data: {
    code: '',
    userImg: '',
    navigationHeight: GlobalData.navigationHeight,
    boxHeight: 0,
    inviteImg: '',
    currencyName: GlobalData.currencyName,
    inviterScore: GlobalData.invite.invitee_score,
    name: '{{NAME}}',
  },
  async onLoad() {
    if (!isLogin()) {
      await checkAuth()
    }
    const user = wx.getStorageSync(`${getServer()}/logged`).data || {}
    const userImg = user.avatar.url || user.avatar
    const boxHeight = wx.getSystemInfoSync().windowHeight
    const code = wx.getStorageSync(`${getServer()}/logged`).data.invite_code
    this.setData({
      userImg,
      code: code,
      boxHeight,
    })
    this.getInviteImg(code)
  },
  onShow() {
    $event.on('after:boot', this, () => {
      this.getPosition()
    })
    this.getPosition()
  },
  onShareAppMessage() {
    return {
      path: `/pages/feed/list/list?code=${this.data.code}`,
    }
  },
  getPosition() {
    this.setData({
      navigationHeight: GlobalData.navigationHeight,
      currencyName: GlobalData.currencyName,
      inviterScore: GlobalData.invite.invitee_score,
    })
  },
  getInviteImg(code: string) {
    const _this = this
    wx.downloadFile({
      url: `${servers[getServer()]
        }/api/v2/wechat/mini-program/wxacodeunlimit?params={"scene":"${code}"}`,
      success(res) {
        if (res.statusCode === 200) {
          _this.setData({
            inviteImg: res.tempFilePath,
          })
        }
      },
    })
  },
})
